/**
 * 药品排名弹窗模块，点击图表首页后初始化弹窗数据
 * 
 * @Date: 2016-1-25
 * @author: Jackson wang
 * @version: 1.0
 * @since: 1.0
 */

define(["jquery","sysmanMain","valueUtil","panelUtil","btnServ","datagridServ","formServ"],
		function($,sysmanMain,valueUtil,panelUtil,btnServ,datagridServ,formServ) {

		var rootid = 'panel-drug-counting-pop-drug';
		
		var window_id = 'drug-counting-pop-win';
		
		var module_code ='drugCountingInfo';
		var module_code_head = 'drugCounting';
		
		var url = '/drugCounting/query/getDrugCount.action';
		var queryParam ;
		
		var _buttonConfig;
		var public_method ={
			init:function(_options){
				
				_buttonConfig = {
					'返回':private_method.backWay,
					'执行查询':private_method.doSearch
				}
				var _qryParam = {
					drug_spec : 'all',
					drug_type : 'all',
					order     : 'drug_sum'
				}
				
				$('#' + rootid + ' .condition-form').form('load',_qryParam);
				var _grid = $('#'+rootid + ' .dg-list')
				var _param = {
					url			: '/'+sysmanMain.getRootPath()+ url,
					rownumbers  : true,
					animate		: true,						
					striped		: true,
					border		: true,					
					idField		: 'id',						
					fitColumns	: true,
					columns		: [[
						{field:'drug_code',title:'药品代码',halign:'center',width:30},        
						{field:'drug_name',title:'药品名称',width:30,align:'center',halign:'center'},
						{field:'drug_spec',title:'药品规格',halign:'center',width:30},        
						{field:'drug_unit',title:'计量单位',halign:'center',width:30},
						{field:'drug_price',title:'药品价格',width:30,align:'center',halign:'center'},
						{field:'outp_drug_sum',title:'门诊用量',halign:'center',width:30},        
						{field:'outp_drug_price',title:'门诊金额',width:30,align:'center',halign:'center'},
						{field:'inp_drug_sum',title:'住院用量',halign:'center',width:30},        
						{field:'inp_drug_price',title:'住院金额',width:30,align:'center',halign:'center'},
						{field:'drug_sum',title:'合计用量',halign:'center',width:30},        
						{field:'total_price',title:'合计金额',width:30,align:'center',halign:'center'}
					]]
				}
				
				var _queryParams = $.extend({},_qryParam,private_method.getCondition())
				
				//获取查询参数
				_param.queryParams={condition:_queryParams}
				_grid.datagrid(_param);
				
				//绑定查询事件
				private_method.bindBtnEvent();
			}
			,
			
			openPageDrug:function(_options){
				
				var config = {
					title	: '药品排名',
					href	: '/'+sysmanMain.getRootPath()+'/' + module_code_head + '/query/popPageDrug.action',
					window	: $('#' + window_id),
					maximized : true,
					flag	: false,
					onClose : private_method.closePage
				}
				queryParam = _options;
				var _param = $.extend({},config,_options)
				datagridServ.windowEditCenter(_param);
			}
			
			,
			/**
			 * 药占比弹窗
			 */
			openPagePercent:function(_options){
				
				var config = {
					title	: '药占比',
					href	: '/'+sysmanMain.getRootPath()+'/' + module_code_head + '/query/popPageDrugPercent.action',
					window	: $('#' + window_id),
					width	: '900',
					height	: '700',
					flag	: false,
					onClose : private_method.closePage
				}
				queryParam = _options;
				var _param = $.extend({},config,_options)
				datagridServ.windowEditCenter(_param);
			}
			,
			
			
			
			rowClick:function(index,data){
				
				if(window.console)
					console.info('inpatient rowClick event fired ...')
				var _config = sysmanMain.getModuConf('inpatientItemFee');
				if(_config ==null){
					_config = {}					
				}	
				
				_config.initQueryParams = {field:'patient_id',fieldVal:data.patient_id}
				sysmanMain.setModuConf('inpatientItemFee',_config)
				sysmanMain.medicalInpatientItemFeeSelect()
			},
			clickBizRow:function(index,row){
				var target, aim
				var a1 = $('#' + rootid + ' div.' + 'list-panel')
				var a2 = $('#' + rootid + ' div.' + 'dept-panel')
				var a3 = $('#' + rootid + ' div.' + 'doct-panel')
				if(panelUtil.getPanelState({target:a1}) == 1){
					aim = "dept-panel"
					private_method.changePanel({aimPanel:aim})
					var _param =$.extend({},queryParam,{dept_code:row.dept_code,data_type:'3'})
					private_method.initDetail(_param)
				}
				else if(panelUtil.getPanelState({target:a2}) == 1){
					aim = "doct-panel"
					private_method.changePanel({aimPanel:aim})
					var _param =$.extend({},{dept_code:row.dept_code,monitor_year_month:queryParam.monitor_year_month,doct_code:row.doct_code})
					private_method.initPatient(_param)
				}
				if(window.console){
					console.info('biz row clicked...')
				}
			},
			/**
			 * 初始化新增页面
			 * @param _options.record 列表选中行
			 * @param _options.form 页面form对象
			 * @param _options.rootId 页面根节点
			 */
			initMain:function(_options){
				var _rootpath = '/'+sysmanMain.getRootPath()
				
				
				rootid = _options.rootId
				
				if(window.console){
					
					console.log(' drugCounting loading ...')
				}
				var t = sysmanMain.getCurrentModuConfig()
				_buttonConfig = {
//					'金额排名':function(){
//						private_method.queryPrice()
//					},
//					'用量排名':function(){
//						private_method.queryNumber()
//					},
//					'药占比':private_method.queryPriPer,
					'返回':private_method.backWay,
					'执行查询':private_method.doSearch
//					'返回医生列表':
//						function(){
//							private_method.changePanel({aimPanel:'details-panel'})
//						}
				}
				var _deptEl = $('#'+window_id +' #indicator_unit_name')
				if(_deptEl.length==1){
					_deptEl.textbox({
						onClickButton:function(){
							deptSelect.openPage({
								deptid:window_id +' #indicator_unit_id',
								deptname:window_id + ' #indicator_unit_name'
							})
						}
					})
				}
				
			},
			buttonExtends:function(){
				
				return _buttonConfig
			}
			
		}
		
		var private_method ={
			backWay:function(){
				var _t =  $('#'+window_id + ' div.details-panel table.dg-list')
				_t.empty();
				private_method.changePanel({aimPanel:"list-panel"})
			},
			
			getCondition:function(){
				var _form = $('#'+ rootid +' .condition-form ')
				var ret = formServ.getDataObj({
					rootId:rootid,
					form : $(_form)
				});
				
				//统计分类参数
				if(ret.drug_type){
					switch(ret.drug_type){
						case 'all':
						ret.anti = null;
						break;
						case 'anti':
						ret.anti = 1;
						break;
						case 'basic':
						ret.basic = 1;
						break;
						default :
						ret.anti = null;
						break;						
					}					
				}
				
				//药品类型参数
				if(ret.drug_spec && ret.drug_spec=='all'){
					
					ret.drug_spec=null;
					
				}
				return ret;
			}
			,
			//执行查询
			doSearch:function(e){
				var ret = private_method.getCondition();
				
				var $dg = $('#' + rootid + " .dg-list")
				$($dg).datagrid('load', {condition:ret});
				if(e && e.currentTarget){
					$(e.currentTarget).linkbutton('enable');
				}
			}
			,
			queryPrice:function(){
				console.info('got queryPrice button!')
				var tempConf = sysmanMain.getModuConf(module_code)
				var newConf = {condition:tempConf.initQueryParams.condition, orderBy:'price', groupBy:tempConf.initQueryParams.groupBy}
				sysmanMain.setModuConf(module_code, {initQueryParams:newConf})
				$dg = $('#' + rootid).find("table.dg-list")
				$($dg).datagrid('load', newConf);
			},
			queryNumber:function(){
				console.info('got queryNumber button!')
				var tempConf = sysmanMain.getModuConf(module_code)
				var newConf = {condition:tempConf.initQueryParams.condition, orderBy:'number', groupBy:tempConf.initQueryParams.groupBy}
				sysmanMain.setModuConf(module_code, {initQueryParams:newConf})
				$dg = $('#' + rootid).find("table.dg-list")
				$($dg).datagrid('load', newConf);
			},
			queryPriPer:function(){
				console.info('got queryPriPer button!')
				var tempConf = sysmanMain.getModuConf(module_code)
				var newConf = {condition:tempConf.initQueryParams.condition, orderBy:'priPer', groupBy:tempConf.initQueryParams.groupBy}
				sysmanMain.setModuConf(module_code, {initQueryParams:newConf})
				$dg = $('#' + rootid).find("table.dg-list")
				$($dg).datagrid('load', newConf);
			},
			initDetail:function(_options){
				//定位明细列表
				var _t =  $('#'+window_id + ' div.details-panel table.dg-list')
				//清空明细列表
				console.log(_t.datagrid('options').url);
				if(!_t.datagrid('options') || !_t.datagrid('options').url){
					//根据条件加载明细列表
					_t.datagrid({
						url:'/'+sysmanMain.getRootPath()+'/' + module_code_head + 'query/getListDomain.action',
						queryParam:{condition:_options}
					})
				}else{
					_t.datagrid('load',{condition:_options})
				}
			},
			initPatient:function(_options){
				//定位明细列表
				var _t =  $('#'+window_id + ' div.patient-panel table.dg-list')
				//清空明细列表
				//console.log(_options.url);
				_t.datagrid({url:'/'+sysmanMain.getRootPath()+'/' + 'baseOutpatient' + '/query/getListPatientDomain.action',
				queryParams:{condition:_options}})
			},
			/**
			 * 
			 */
			changePanel:function(_options){
				var tp
				if(_options.aimPanel){
					tp = $('#' + rootid + ' div.easyui-panel')
					if (tp.length > 0) {
						tp.panel('close')
						console.info("here all closed")
					}
					tp = $('#' + rootid + ' div.' + _options.aimPanel)
					tp.panel('open')
				}
				else{
					var $listPanel = $('#' +window_id +'  div.list-panel' )
					var $detailsPanel = $('#' +window_id +' div.details-panel' )
					
					panelUtil.togglePanel({target:$listPanel})
					panelUtil.togglePanel({target:$detailsPanel})
				}
			},
			initEditList:function(){
				
			},
			closePage:function(){
				$('#'+window_id).empty();
			},
			bindBtnEvent:function(){

				var window_btn = $('#'+rootid +' a.function-btn')
				$(window_btn).unbind('click')
				$(window_btn).bind('click',	private_method.function_btn_listenner)

			},
			detailInfo:function(patient_id){
				var _config={}
				_config.initQueryParams = {condition:{outpatient_number:patient_id}}
				sysmanMain.setModuConf('outpatientItemFee',_config)
				sysmanMain.medicalOutpatientItemFeeSelect()
			},

			// 功能按钮监听函数
			function_btn_listenner : function(e) {
				if(window.console){
					console.log('got a return here~~~');
				}
				btnServ.applyButton({buttonEvt:e,buttonConfig:_buttonConfig})

			}
		}
			
		
		return public_method;
			
})